package com.barcode.code;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;

/**
 * 类描述:
 *生成条形码，不带文字
 * @author jianweiwang
 * @version v1.0
 * @link wjw@choicesoft.com.cn
 * @date 2019/4/1 10:33
 **/
public class coding {
    public static void main(String[] args) throws Exception {
//        int width = 300;
//        int height = 300;
         int width = 105;
         int height = 50;
        // 条形码的输入是13位的数字
         String text = "6923450657713";
        // 二维码的输入是字符串
//        String text = "test生成条形码图片";
        String format = "png";
        HashMap<EncodeHintType, Object> hints = new HashMap<>(3);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        // 容错级别 这里选择最高H级别
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        // 条形码的格式是 BarcodeFormat.EAN_13
        // 二维码的格式是BarcodeFormat.QR_CODE
        BitMatrix bm = new MultiFormatWriter().encode(text, BarcodeFormat.CODE_128, width, height, hints);
//        File out = new File("new.png");
        // 生成条形码图片
         File out = new File("ean3.png");
        Path file = out.toPath();
        MatrixToImageWriter.writeToPath(bm,format,file);
    }
}
